SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO


CREATE PROCEDURE [dbo].[SP_TMPLAN_EMPR_Q04]
/*--------------------------------------------------------------*/  
/*----- Empresa           : OFISIS S.A.                     ----*/  
/*----- Cliente           : OFISIS S.A.                     ----*/  
/*----- Sistema           : Contabilidad                    ----*/  
/*----- Modulo            : Administracion                  ----*/  
/*----- Programa          : Validacion del Plan de Cuentas  ----*/  
/*-----                     Valida Quiebres                 ----*/  
/*----- Nombre            : SP_TMPLAN_EMPR_Q04              ----*/  
/*----- Script            : TMPLAN_EMPR                     ----*/  
/*----- Desarrollado por  : Martin Garcia Farfan            ----*/  
/*----- Fecha             : 19/08/1999                      ----*/  
/*----- Base de Datos     : MS SQL Server                   ----*/  
/*----- Version BD        : 7.0                             ----*/  
/*--------------------------------------------------------------*/  
/*----  MODIF POR : PERCY GARCIA   ----*/  
/*---- FECHA MODIF : 12/02/2004     ----*/  
/*---- DETALLE MODIF : A?O 9999 EN JOINS ----*/   
/*--------------------------------------------------------------*/  
/*----- DROP PROCEDURE SP_TMPLAN_EMPR_Q04                   ----*/  
/*----- GRANT ALL ON SP_TMPLAN_EMPR_Q04 TO PUBLIC           ----*/  
/*----- CALL SP_TMPLAN_EMPR_Q04 '01'                            ----*/  
/*--------------------------------------------------------------*/  
 @ISCO_EMPR       Varchar(02)  
As  
  
 Declare  
  @VSDE_QUIE        Varchar(20),  
  @VNNU_QUIE        Int,  
  @VNNU_INFO        Int,  
  @CSCO_CNTA_EMPR   Varchar(10),  
  @CSDE_CNTA_EMPR   Varchar(80),  
  @VSCO_CNTA        Varchar(10),  
  @VNNU_TOTA        Int,  
  @VNNU_CONT        Int  
  
 Create table #TWPLAN_EMPR_Q04  
  (CO_CNTA_EMPR   Varchar(10),  
   DE_CNTA_EMPR   Varchar(80),  
   NU_QUIE        int)  
  
 Select @VNNU_INFO = NV_INFO_CNTA,   
        @VSDE_QUIE = NV_QUIE_CNTA  
  From TMPARA_CONT  
  Where CO_EMPR = @ISCO_EMPR  
  and NU_ANNO = 9999--YEAR(GETDATE())  
  
 Declare CU_TMPLAN_EMPR_Q04_1 Cursor  
  For  
 /*   Select CO_CNTA_EMPR, DE_CNTA_EMPR  
    From TMPLAN_EMPR  
     Where CO_EMPR = @ISCO_EMPR  
      And Datalength(CO_CNTA_EMPR) = @VNNU_INFO  
      and NU_ANNO =YEAR(GETDATE())*/  
  
    Select CO_CNTA_EMPR, DE_CNTA_EMPR  
    From TMPLAN_ANNO  
     Where CO_EMPR = @ISCO_EMPR  
      And Datalength(CO_CNTA_EMPR) = @VNNU_INFO  
     And NU_ANNO = 9999  
   
 Select @VNNU_CONT = 0  
 Select @VNNU_QUIE = 0  
 Select @VNNU_TOTA = 0  
  
 Open CU_TMPLAN_EMPR_Q04_1  
 Fetch CU_TMPLAN_EMPR_Q04_1 INTO @CSCO_CNTA_EMPR, @CSDE_CNTA_EMPR  
    
 While @@Fetch_Status = 0   
  Begin   

   Select @VNNU_CONT = Datalength(RTRIM(@VSDE_QUIE));     
   Select @VNNU_CONT = @VNNU_CONT - 2;  
   Select @VNNU_QUIE = Convert(int, SUBSTRING(@VSDE_QUIE, @VNNU_CONT, 1));  
   While @VNNU_CONT > 0  
    Begin  
     Select @VSCO_CNTA = Substring(@CSCO_CNTA_EMPR, 1, @VNNU_QUIE);  
/*     Select @VNNU_TOTA = COUNT(*)  
      From TMPLAN_EMPR  
     Where CO_EMPR = @ISCO_EMPR  
     And CO_CNTA_EMPR = @VSCO_CNTA  
     And Datalength(CO_CNTA_EMPR) = @VNNU_QUIE  
     and NU_ANNO =YEAR(GETDATE());*/  
  
     Select @VNNU_TOTA = COUNT(*)  
      From TMPLAN_ANNO  
     Where CO_EMPR = @ISCO_EMPR  
     And CO_CNTA_EMPR = @VSCO_CNTA  
     And NU_ANNO = 9999  
     And Datalength(CO_CNTA_EMPR) = @VNNU_QUIE;  
  
    IF @VNNU_TOTA = 0  
      Insert into #TWPLAN_EMPR_Q04(CO_CNTA_EMPR,   
         DE_CNTA_EMPR, NU_QUIE)  
       Values(@VSCO_CNTA, @CSDE_CNTA_EMPR, @VNNU_QUIE)  
  
     Select @VNNU_CONT = @VNNU_CONT - 2;  
     Select @VNNU_QUIE = Convert(INT, Substring(@VSDE_QUIE, @VNNU_CONT, 1));  
   End  
       
   Fetch CU_TMPLAN_EMPR_Q04_1 INTO @CSCO_CNTA_EMPR, @CSDE_CNTA_EMPR  
 End  
  
 Close CU_TMPLAN_EMPR_Q04_1  
 Deallocate CU_TMPLAN_EMPR_Q04_1  
  
Select * from #TWPLAN_EMPR_Q04  
 Order By CO_CNTA_EMPR  
  
Return  
/*------------------------- Fin ------------------------------*/  
  





GO
